//
//  WelcomeViewController.swift
//  CXCWeibo
//
//  Created by cxc on 15/6/26.
//  Copyright © 2015年 china. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        if let urlString = sharedUserAccount?.avatar_large{
            iconView.sd_setImageWithURL(NSURL(string: urlString))
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        bottomConstraint?.constant = -(UIScreen.mainScreen().bounds.height - 200)
        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
            self.view.layoutIfNeeded()
            }) { _ in
                UIView.animateWithDuration(1.0, animations: { () -> Void in
                    self.welcomeLaber.alpha = 1.0
                    }, completion: { (_) -> Void in
                        NSNotificationCenter.defaultCenter().postNotificationName(CCSwitchRootVCNotification, object: true)
                })
        }
    }
    
    var bottomConstraint: NSLayoutConstraint?
    
    // MARK: - 设置界面控件
    override func loadView() {
        view = UIView()
        let backImageView = UIImageView(image: UIImage(named: "ad_background"))
        view.addSubview(backImageView)
        view.addSubview(iconView)
        view.addSubview(welcomeLaber)
        
        // 设置自动布局
        backImageView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[subView]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView" : backImageView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[subView]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView" : backImageView]))
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        bottomConstraint = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -160)
        view.addConstraint(bottomConstraint!)
        
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subView(90)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView" : iconView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subView(90)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView" : iconView]))
        
        welcomeLaber.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: welcomeLaber, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: welcomeLaber, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 8))
    }
    
   
   
    // MARK:  - 懒加载
    lazy var iconView:UIImageView = {
        let iconView = UIImageView(image: UIImage(named:  "avatar_default_big"))
        
        iconView.layer.cornerRadius = 42.5
        iconView.layer.masksToBounds = true
        
        return iconView
    }()
    lazy var welcomeLaber:UILabel = {
        let laber = UILabel()
        laber.text = "欢迎归来"
        laber.sizeToFit()
        
        laber.alpha = 0
        return laber
    }()

}
